package com.edu.bhu.admin.controller.admin;

import java.util.List;

import com.edu.bhu.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.edu.bhu.common.annotation.Log;
import com.edu.bhu.common.enums.BusinessType;
import com.edu.bhu.admin.domain.PreBody;
import com.edu.bhu.admin.service.IPreBodyService;
import com.edu.bhu.common.core.controller.BaseController;
import com.edu.bhu.common.core.domain.AjaxResult;
import com.edu.bhu.common.utils.poi.ExcelUtil;
import com.edu.bhu.common.core.page.TableDataInfo;

/**
 * 身体状况Controller
 * 
 * @author yutianyang
 * @date 2022-04-23
 */
@Controller
@RequestMapping("/admin/body")
public class PreBodyController extends BaseController
{
    private String prefix = "admin/body";

    @Autowired
    private IPreBodyService preBodyService;

    @RequiresPermissions("admin:body:view")
    @GetMapping()
    public String body()
    {
        return prefix + "/body";
    }

    @GetMapping("myBody")
    public String myBody()
    {
        return prefix + "/myBody";
    }

    /**
     * 查询身体状况列表
     */
    @RequiresPermissions("admin:body:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PreBody preBody)
    {
        startPage();
        List<PreBody> list = preBodyService.selectPreBodyList(preBody);
        return getDataTable(list);
    }

    /**
     * 查询我的身体状况列表
     */
    @RequiresPermissions("admin:body:list")
    @PostMapping("/myList")
    @ResponseBody
    public TableDataInfo myList(PreBody preBody)
    {
        preBody.setUserId(ShiroUtils.getSysUser().getUserId());
        startPage();
        List<PreBody> list = preBodyService.selectPreBodyList(preBody);
        return getDataTable(list);
    }

    /**
     * 导出身体状况列表
     */
    @RequiresPermissions("admin:body:export")
    @Log(title = "身体状况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PreBody preBody)
    {
        List<PreBody> list = preBodyService.selectPreBodyList(preBody);
        ExcelUtil<PreBody> util = new ExcelUtil<PreBody>(PreBody.class);
        return util.exportExcel(list, "身体状况数据");
    }

    /**
     * 新增身体状况
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存身体状况
     */
    @RequiresPermissions("admin:body:add")
    @Log(title = "身体状况", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PreBody preBody)
    {
        preBody.setUserId(ShiroUtils.getSysUser().getUserId());
        return toAjax(preBodyService.insertPreBody(preBody));
    }

    /**
     * 修改身体状况
     */
    @RequiresPermissions("admin:body:edit")
    @GetMapping("/edit/{bodyId}")
    public String edit(@PathVariable("bodyId") Long bodyId, ModelMap mmap)
    {
        PreBody preBody = preBodyService.selectPreBodyByBodyId(bodyId);
        mmap.put("preBody", preBody);
        return prefix + "/edit";
    }

    /**
     * 修改保存身体状况
     */
    @RequiresPermissions("admin:body:edit")
    @Log(title = "身体状况", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PreBody preBody)
    {
        return toAjax(preBodyService.updatePreBody(preBody));
    }

    /**
     * 删除身体状况
     */
    @RequiresPermissions("admin:body:remove")
    @Log(title = "身体状况", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(preBodyService.deletePreBodyByBodyIds(ids));
    }
}
